package com.slz3.business.module.memberManagement.memShippingAddress.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.memShippingAddress.domain.MemShippingAddress;
import com.slz3.business.module.memberManagement.memShippingAddress.domain.MemShippingAddressFactory;
import com.slz3.business.module.memberManagement.memShippingAddress.steward.MemShippingAddressSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MemShippingAddressServiceImpl implements MemShippingAddressService {

    @Override
    public MemShippingAddress save(MemShippingAddress memShippingAddress) {
        return Repositorys.memShippingAddress.save(MemShippingAddressFactory.initBaseFields(memShippingAddress));
    }

    @Override
    public Page<MemShippingAddress> page(MultiValueMap<String, String> params) {
        Page<MemShippingAddress> memShippingAddressPage = Repositorys.memShippingAddress.findAll(
                MemShippingAddressSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return memShippingAddressPage;
    }

    @Override
    public MemShippingAddress modify(MemShippingAddress memShippingAddressNew) {
        return Repositorys.memShippingAddress.saveAndFlush(MemShippingAddressFactory.initBaseFields(memShippingAddressNew));
    }

    @Override
    public Optional<MemShippingAddress> single(UUID uuid) {
        return Repositorys.memShippingAddress.findById(uuid);
    }

    @Override
    public List<MemShippingAddress> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.memShippingAddress.findAllById(uuids);
    }

    @Override
    public List<MemShippingAddress> delete(List<MemShippingAddress> memShippingAddressList) {
        memShippingAddressList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MemShippingAddressFactory.initBaseFields(item);
        });
        return Services.memShippingAddressTransactional.batchModify(memShippingAddressList);
    }

}
